package com.qf.git04.demo02;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //解决post中文乱码问题
        req.setCharacterEncoding("utf-8");
        //获取表单中的数据
        String uname = req.getParameter("uname");
        String pwd = req.getParameter("pwd");
        //登陆验证
        if("admin".equals(uname)&&"123".equals(pwd)){
            //创建session对象
            HttpSession session = req.getSession();
            session.setAttribute("user",new User(uname,pwd));
            //响应客户端
            resp.getWriter().println("login success");
        }else {
            //响应客户端
            resp.getWriter().println("login fail");
        }
    }
}
